#include "commentsProcessor.h"
#include "jsEngine.h"
#include "geocoder.h"

void CommentsProcessor::run(string path, LjPost* rootComment, vector<Place>* places){
	filePath_ = path;
	rootComment_ = rootComment;

	//reading js code
	ifstream input_file;
   	input_file.open(filePath_.c_str());
	string jsCode ( (istreambuf_iterator<char>(input_file)),
                    (istreambuf_iterator<char>()) );
	input_file.close();

	Geocoder geocoder;
	geocoder.setCityName(rootComment->getCity());	

	//running jsEngine
	JsEngine engine;
	engine.setGeocoder(&geocoder);	
	engine.init(jsCode);

	vector<Comment*> subComments = rootComment->getSubComments();
	for(vector<Comment*>::iterator it = subComments.begin(); it != subComments.end(); ++it){
		engine.runJS(jsCode, (*it), places);		
	}


	//debug: only 1-st
	//vector<Comment*>::iterator it = rootComment->getSubComments().begin();
	//engine.runJS(jsCode, (*it)->getText());
}


